<?php


class UserTworldCardModel extends BaseModel
{
    protected $table = 'yf_user_tworld_card';

    protected $principal = 'id';

    /**
     * 状态
     */
    const STATUS = [
        'normal' => 1,//正常
        'overdue' => 2,//过期
        'back' => 3,//退卡
    ];

    /**
     * 根据会员id和会员卡 获取我的会员卡的信息
     * @param int $user_id
     * @param int $tworld_card_id
     * @return array|bool|mixed
     */
    public function getByUserAndCardid(int $user_id, int $tworld_card_id)
    {
        return $this->get(['id', 'number_code', 'status', 'expiration_time'], [
                'user_id' => $user_id,
                'tworld_card_id' => $tworld_card_id,
                'is_del' => 0
            ]) ?? [];
    }

    public function getByUser(int $user_id)
    {
        return $this->get(['id', 'number_code', 'status', 'expiration_time'], [
                'user_id' => $user_id,
                'is_del' => 0
            ]) ?? [];
    }

    /**
     * @param int $user_id
     * @return array
     */
    public function getAllByUser(int $user_id): array {
        return $this->findAll([
           'user_id' => $user_id,
           'is_del' => 0
        ]) ?? [];
    }

    /**
     * 修改卡状态-过期
     * @param int $id
     * @return bool|PDOStatement|string
     */
    public function updateStatusToPast(int $id)
    {
        return $this->updateById([
            'status' => self::STATUS['overdue']
        ], $id);
    }

}